import pygame as pg
from pygame.locals import *
import classes
from costants import *
from myTimer import Timer
import buttons
##########################################################
##########################################################
pg.font.init()
try: big_font=pygame.font.Font(FONT,90)
except IOError: big_font=pygame.font.Font(ALTERNATIVE_FONT,90)
##########################################################
##########################################################
def play(screen,ark_level,gioc):
	if ark_level>=3: ark_level=2
	gioc.remaining_balls=3-ark_level
	gioc.starting_game(ARK)
	timer=Timer(60)
	sfondo=pg.image.load(BG_IMAGE).convert()
	ark=pg.image.load(ARK_IMAGE).convert()
	while ark.get_width()<600 and ark.get_height()>420:
		ark=pg.transform.scale(ark,(ark.get_width()+2,ark.get_height()-2))
		screen.blit(sfondo,(0,0))
		screen.blit(ark,(15,15))
		pg.display.update()
	screen.blit(sfondo,(0,0))
	pg.display.update()
	field=classes.Field(screen)
	bar=classes.Bar(screen)
	ball=classes.Ball(0,0,screen)
	ball.on_bar(bar)
	clock=pg.time.Clock()
	classes.blit_all(screen,sfondo,field,bar,ball,timer)
	gioc.blit(screen)
	pg.display.update()
	for i in range(3-ark_level):
		ball=classes.Ball(0,0,screen)
		bar=classes.Bar(screen)
		ball.on_bar(bar)
		ball.on_bar(bar)
		space_free=1
		timer.stop()
		while ball.state=='alive':
			dt=clock.tick(30)
			events = pg.event.get()
			for e in events:
				if e.type==QUIT: return EXIT_GAME
			keys=pg.key.get_pressed()
			if keys[K_ESCAPE]:
				ball.active=False
				ball.on_bar(bar)
				timer.stop()
				pause_exit=pause(screen)
				if pause_exit==CONTINUE:
					screen.blit(sfondo,(0,0))
					classes.blit_all(screen,sfondo,field,bar,ball,timer)
					gioc.blit(screen)
					pg.display.update()
				else: return pause_exit
			if ball.active:
				ball.move(dt/ARK_SPEED,bar)
			else: ball.on_bar(bar)
			if keys[K_SPACE] and not ball.active:
				timer.start()
				timer.start()
				ball.active=True
			classes.blit_all(screen,sfondo,field,bar,ball,timer)
			gioc.blit(screen)
			pg.display.update()
			if keys[K_LEFT]: bar.move_left(dt)
			if keys[K_RIGHT]: bar.move_right(dt)
			if timer.remaining_time==-1:
				while 1:
					timer.start()
					lost=big_font.render("YOU WIN!",True,LIGHTYELLOW)
					screen.blit(lost,lost.get_rect(center=ARK_CENTER))
					pg.display.update()
					if timer.remaining_time==-2:
						timer.stop()
						while ark.get_width()>420 and ark.get_height()<600:
							ark=pg.transform.scale(ark,\
							    (ark.get_width()-2,ark.get_height()+2))
							screen.blit(sfondo,(0,0))
							screen.blit(ark,(15,15))
							pg.display.update()
						return WON
		gioc.ball_lost()
	time=timer.remaining_time
	while 1:
		timer.start()
		lost=big_font.render("YOU LOST!",True,(255,255,102))
		screen.blit(lost,lost.get_rect(center=ARK_CENTER))
		screen.blit(sfondo,ARK_BOTTOM_RECT,ARK_BOTTOM_RECT)
		gioc.blit(screen)
		pg.display.update()
		if timer.remaining_time==time-2: 
			timer.stop()
			while ark.get_width()>420 and ark.get_height()<600:
				ark=pg.transform.scale(ark,(ark.get_width()-2,ark.get_height()+2))
				screen.blit(sfondo,(0,0))
				screen.blit(ark,(15,15))
				pg.display.update()
			return LOST
##########################################################
def pause(screen):
	game_pause=True
	menu_bg=pg.image.load(MENU_BG).convert()
	screen.blit(menu_bg,(0,0))
	pg.display.update()
	continue_game=buttons.Button((430,410),"CONTINUE GAME",screen)
	back_to_main=buttons.Button((430,460),"BACK TO MAIN",screen)
	quit=buttons.Button((430,510),"EXIT",screen)
	continue_game.blit()
	back_to_main.blit()
	quit.blit()
	while game_pause:
		event=pg.event.wait()
		if event.type==QUIT: return EXIT_GAME
		if event.type==MOUSEBUTTONDOWN:
			if continue_game.is_pressed(event.dict): return CONTINUE
			if back_to_main.is_pressed(event.dict): return TO_MAIN
			if quit.is_pressed(event.dict): return EXIT_GAME
##########################################################
##########################################################
